<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <%@ include file="/static/admin/jsp/include.jsp"%>
    <script type="text/javascript">
        var id = "${id}";
        var grid;
        /*var submitForm = function($dialog, $grid, $pjq) {
            if ($('form').form('validate')) {
                var obj=sy.serializeObject($('form'));
                var url=sy.contextPath + '/problemAns/save';
                $.post(url, obj, function(result) {
                    if (result.code == 0) {
                        $grid.datagrid('reload');
                        $dialog.dialog('destroy');
                    } else {
                        $pjq.messager.e('保存失败,'+result.msg);
                    }
                }, 'json');
            }
        };*/



        $(function() {
            grid = $('#grid').datagrid({
                url : sy.contextPath + '/problemAns/findList?id='+id,
                columns : [ [ {
                    width : $(this).width() * 0.1,
                    title : '回答',
                    field : 'answer',
                    align : 'center',
                },{
                    width : $(this).width() * 0.05,
                    title : '排序',
                    field : 'sortOrder',
                    align : 'center',
                }
                ] ]
            });


            $('#type').combobox({
                onSelect: function(rec){
                    grid.datagrid('load',sy.serializeObject($('#searchForm')));
                }
            });
        });

        //添加
        var addFun = function($dialog, $grid, $pjq) {
            var dialog = parent.sy.modalDialog({
                title : '新增回答',
                width : 600,
                height : 300,
                url : sy.contextPath + '/go?path=problem/problemAnsEdit&problemId='+id,
                buttons : [ {
                    text : '保存',
                    handler : function() {
                        dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
                    }
                } ]
            });
        };

        //添加
        var editFun = function($dialog, $grid, $pjq) {
            var dialog = parent.sy.modalDialog({
                title : '修改',
                width : 600,
                height : 300,
                url : sy.contextPath + '/go?path=problem/problemAnsEdit&problemId='+id,
                buttons : [ {
                    text : '保存',
                    handler : function() {
                        dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
                    }
                } ]
            });
        };

        //修改
        var editFun = function($dialog, $grid, $pjq) {
            var rows = grid.datagrid('getSelections');
            if (rows.length != 1) {
                parent.$.messager.w('请选择一条记录进行编辑！');
                return;
            }
            var dialog = parent.sy.modalDialog({
                title : '修改',
                width : 800,
                height : 600,
                url : sy.contextPath + '/go?path=problem/problemAnsEdit&id=' + rows[0].id+'&problemId='+id,
                buttons : [ {
                    text : '保存',
                    handler : function() {
                        dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
                    }
                } ]
            });
        };

        var delFun = function() {
            var rows = grid.datagrid('getSelections');
            if (rows.length == 0) {
                parent.$.messager.w('请选择需要删除的记录！');
                return;
            }
            parent.$.messager.confirm('询问', '您确定要删除此记录？', function(r) {
                if (r) {
                    var ids = [];
                    for ( var i = 0, l = rows.length; i < l; i++) {
                        var r = rows[i];
                        ids.push(r.id);
                    }
                    var id = ids.join(',');

                    $.post(sy.contextPath + '/problemAns/del', {
                        id : id
                    }, function() {
                        rows.length = 0;//必须，否则有bug
                        grid.datagrid('reload');
                    }, 'json');
                }
            });
        };
    </script>
</head>
<body>
<div id="toolbar">
    <form id="searchForm">
        <div>

        </div>
    </form>
    <div class="tbbutton">

        <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-plus-circle',plain:true" onclick="addFun();">添加</a>
        <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-pencil',plain:true" onclick="editFun();">修改</a>
        <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-trash',plain:true" onclick="delFun();">删除</a>

    </div>
</div>
<table id="grid" data-options="fit:true,border:false"></table>
</body>
</html>